//
//  BZMeHeaderView.m
//  BZMeComponent
//
//  Created by 郭艺伟 on 2017/10/11.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "BZMeHeaderView.h"
#import <Masonry/Masonry.h>
#import <libextobjc/EXTScope.h>
#import <BZBundleKit/BZImageKit.h>
#import <BZBundleKit/BZBundleResourcesKit.h>
#import "BZMeComponentHeader.h"
@implementation BZMeHeaderView

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        @weakify(self);
        UIImageView *bgView = [[UIImageView alloc]init];
        //bgView.image = [UIImage imageNamed:@"pic_personbg"];
        //[BZImageKit imageWithName:@"pic_personbg" type:@"png" inBundle:@"BZResourcesBundle" inDirectory:@"ios/Me"];
        bgView.image = [BZBundleResourcesKit imageWithName:@"pic_personbg"
                                       type:kBZMeImageType
                                 bundleName:kBZMeBundleName
                                inDirectory:kBZMeBundleDirectory];
        [self addSubview:bgView];
        
        UILabel *rankLabel = [[UILabel alloc]init];
        rankLabel.textColor = [UIColor whiteColor];
        rankLabel.text = @"会员等级:25";
        rankLabel.textAlignment = NSTextAlignmentCenter;
        rankLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:rankLabel];
        self.rankL = rankLabel;
        
        
        UIImageView *avatarV = [[UIImageView alloc]init];
        //avatarV.image = [UIImage imageNamed:@"icon_head"];
        avatarV.image =
        [BZBundleResourcesKit imageWithName:@"icon_head"
                                       type:kBZMeImageType
                                 bundleName:kBZMeBundleName
                                inDirectory:kBZMeBundleDirectory];
        avatarV.layer.cornerRadius = 50;
        avatarV.layer.masksToBounds = YES;
        avatarV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickAvatar)];
        [avatarV addGestureRecognizer:tap];
        [self addSubview:avatarV];
        self.avartarV = avatarV;
        
        UILabel *nickNameL = [[UILabel alloc]init];
        nickNameL.textColor = [UIColor whiteColor];
        nickNameL.textAlignment = NSTextAlignmentCenter;
        nickNameL.text = @"";
        nickNameL.font = [UIFont systemFontOfSize:16];
        [self addSubview:nickNameL];
        self.nickL = nickNameL;
        
        UIButton *editBtn = [[UIButton alloc]init];
        editBtn.hidden = YES;
        
        UIImage *editImage =
        [BZBundleResourcesKit imageWithName:@"btn_nameedit"
                                       type:kBZMeImageType
                                 bundleName:kBZMeBundleName
                                inDirectory:kBZMeBundleDirectory];
        //[UIImage imageNamed:@"btn_nameedit"]
        [editBtn setImage:editImage forState:UIControlStateNormal];
        [editBtn addTarget:self action:@selector(didClickEditNickNameBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:editBtn];
        
        
        UILabel *budlarName = [[UILabel alloc]init];
        budlarName.textColor = [UIColor whiteColor];
        budlarName.textAlignment = NSTextAlignmentCenter;
        [self addSubview:budlarName];
        budlarName.text = @"佛友号!";
        budlarName.font = [UIFont systemFontOfSize:13];
        self.buddhalNameL = budlarName;
        
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        
        [avatarV mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.centerX.mas_equalTo(self);
            make.height.mas_offset(100);
            make.width.mas_offset(100);
            make.top.equalTo(self).with.offset(30);
        }];
        
        [nickNameL setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [nickNameL mas_makeConstraints:^(MASConstraintMaker *make) {
            //            make.left.lessThanOrEqualTo(avatarV.mas_left).with.offset(0);
            make.top.equalTo(avatarV.mas_bottom).with.offset(10);
            //            make.right.equalTo(avatarV.mas_right).with.offset(0);
            //            make.right.mas_lessThanOrEqualTo(avatarV.mas_right);
            make.centerX.equalTo(self.mas_centerX);
            make.height.mas_offset(20);
        }];
        [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(nickNameL.mas_right).with.offset(5);
            make.top.equalTo(avatarV.mas_bottom).with.offset(10);
            make.height.mas_offset(22);
            make.width.mas_offset(0);
        }];
        [budlarName mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.left.equalTo(self).with.offset(5);
            make.top.equalTo(editBtn.mas_bottom).with.offset(5);
            make.right.equalTo(self).with.offset(0);
            make.height.mas_offset(15);
        }];
        [rankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.left.equalTo(self).with.offset(0);
            make.right.equalTo(self).with.offset(0);
            make.top.equalTo(budlarName.mas_bottom).with.offset(5);
            make.height.mas_offset(15);
            
        }];
        
        
    }
    return self;
}



- (void)updateNickNameConstrains{
    
    CGSize size = [self.nickL.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    [self.nickL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(size.width+10);
    }];
    
    [self.avartarV mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_offset(100);
    }];
    
}


- (void)didClickAvatar{
    
    
    NSLog(@"click Avatar!!!");
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickAvatarFromView:)]) {
        
        [self.delegate didClickAvatarFromView:self];
    }
    
    
}

- (void)didClickEditNickNameBtn:(UIButton *)editNickName{
    
    NSLog(@"click edit nick name btn!!!");
    
}


@end
